﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Paneles;
using System.Windows.Forms;
using System.Drawing;
using fideeraSoft.Utils;
using fideeraSoft.Entities;
using fideeraSoft.Handlers;

namespace fideeraSoft.PanelesNuevosForms
{
    public class PanelLogeoUsuario : PanelGeneral
    {
        private Label lbUsuario;
        private TableLayoutPanel tlpLogin;
        private TextBox tbSenha;
        private TextBox tbUsuario;
        private Label lbSenha;
        private Button btSubmit;
        private Button btBorrar;
        private TableLayoutPanel tlpBotones;
        
        Form formR;
        public static string CLAVE_TECNICA="34521";
        public static string FULL_USER = "Admin";
        Handler handler;

        public PanelLogeoUsuario(Form form,Handler handler)
        {
            
            formR = form;
            this.handler = handler;

            this.formatearPanelSegunForm(formR);

            this.formatearTableTitulo("Login Usuario");
            this.InitializeComponent();
            
            this.formatearTablaLogin();
            this.formatearTablaBotones();

            this.Controls.Add(tlpLogin);
            this.Controls.Add(tlpBotones);


        }

        private void formatearTablaLogin()
        {
            tlpLogin.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, tlpLogin.Size.Width),InitialRealPointVertical);
            tlpLogin.Controls.Add(lbUsuario);
            tlpLogin.Controls.Add(tbUsuario);

            tlpLogin.Controls.Add(lbSenha);
            tlpLogin.Controls.Add(tbSenha);


        }

        private void formatearTablaBotones()
        {            
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.formR.Size.Width, tlpBotones.Size.Width), InitialRealPointVertical+tlpLogin.Size.Height+20);
            
            tlpBotones.Controls.Add(btBorrar);
            tlpBotones.Controls.Add(btSubmit);

        }



        private void InitializeComponent()
        {
            this.lbUsuario = new System.Windows.Forms.Label();
            this.tlpLogin = new System.Windows.Forms.TableLayoutPanel();
            this.tbSenha = new System.Windows.Forms.TextBox();
            this.tbUsuario = new System.Windows.Forms.TextBox();
            this.lbSenha = new System.Windows.Forms.Label();
            this.btSubmit = new System.Windows.Forms.Button();
            this.btBorrar = new System.Windows.Forms.Button();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.SuspendLayout();
            // 
            // lbUsuario
            // 
            this.lbUsuario.AutoSize = true;
            this.lbUsuario.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbUsuario.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbUsuario.Location = new System.Drawing.Point(0, 0);
            this.lbUsuario.Name = "lbUsuario";
            this.lbUsuario.Size = new System.Drawing.Size(100, 23);
            this.lbUsuario.TabIndex = 0;
            this.lbUsuario.Text = "Usuario";
            this.lbUsuario.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tlpLogin
            // 
            this.tlpLogin.BackColor = System.Drawing.Color.LightCyan;
            this.tlpLogin.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpLogin.ColumnCount = 2;
            this.tlpLogin.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 40F));
            this.tlpLogin.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 60F));
            this.tlpLogin.Location = new System.Drawing.Point(0, 0);
            this.tlpLogin.Name = "tlpLogin";
            this.tlpLogin.RowCount = 2;
            this.tlpLogin.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpLogin.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpLogin.Size = new System.Drawing.Size(300, 100);
            this.tlpLogin.TabIndex = 0;
            // 
            // tbSenha
            // 
            this.tbSenha.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbSenha.Location = new System.Drawing.Point(0, 0);
            this.tbSenha.Margin = new System.Windows.Forms.Padding(3, 10, 3, 3);
            this.tbSenha.MaxLength = 12;
            this.tbSenha.Name = "tbSenha";
            this.tbSenha.PasswordChar = '*';
            this.tbSenha.Size = new System.Drawing.Size(150, 30);
            this.tbSenha.TabIndex = 0;
            // 
            // tbUsuario
            // 
            this.tbUsuario.Dock = System.Windows.Forms.DockStyle.Top;
            this.tbUsuario.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbUsuario.Location = new System.Drawing.Point(0, 0);
            this.tbUsuario.Margin = new System.Windows.Forms.Padding(3, 10, 3, 3);
            this.tbUsuario.MaxLength = 40;
            this.tbUsuario.Name = "tbUsuario";
            this.tbUsuario.Size = new System.Drawing.Size(150, 30);
            this.tbUsuario.TabIndex = 0;
            // 
            // lbSenha
            // 
            this.lbSenha.AutoSize = true;
            this.lbSenha.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbSenha.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbSenha.Location = new System.Drawing.Point(0, 0);
            this.lbSenha.Name = "lbSenha";
            this.lbSenha.Size = new System.Drawing.Size(100, 23);
            this.lbSenha.TabIndex = 0;
            this.lbSenha.Text = "Contraseña";
            this.lbSenha.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // btSubmit
            // 
            this.btSubmit.BackColor = System.Drawing.Color.AliceBlue;
            this.btSubmit.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btSubmit.Location = new System.Drawing.Point(0, 0);
            this.btSubmit.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btSubmit.Name = "btSubmit";
            this.btSubmit.Size = new System.Drawing.Size(75, 40);
            this.btSubmit.TabIndex = 0;
            this.btSubmit.Text = "Ingresar";
            this.btSubmit.UseVisualStyleBackColor = false;
            this.btSubmit.Click += new System.EventHandler(this.btSubmit_Click);
            // 
            // btBorrar
            // 
            this.btBorrar.BackColor = System.Drawing.Color.AliceBlue;
            this.btBorrar.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBorrar.Location = new System.Drawing.Point(0, 0);
            this.btBorrar.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btBorrar.Name = "btBorrar";
            this.btBorrar.Size = new System.Drawing.Size(75, 40);
            this.btBorrar.TabIndex = 0;
            this.btBorrar.Text = "Borrar";
            this.btBorrar.UseVisualStyleBackColor = false;
            this.btBorrar.Click += new System.EventHandler(this.btBorrar_Click);
            // 
            // tlpBotones
            // 
            this.tlpBotones.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Margin = new System.Windows.Forms.Padding(0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 46);
            this.tlpBotones.TabIndex = 0;
            this.ResumeLayout(false);

        }

        private void btBorrar_Click(object sender, EventArgs e)
        {
            tbUsuario.Clear();
            tbSenha.Clear();
        }

        private void btSubmit_Click(object sender, EventArgs e)
        {


            if (!string.IsNullOrWhiteSpace(tbUsuario.Text) && !string.IsNullOrWhiteSpace(tbSenha.Text))
            {


                if ((tbUsuario.Text.Length >= 3) && (tbSenha.Text.Length >= 3))
                {                                        
                    
                    try
                    {   
                        if((tbUsuario.Text.ToString()==FULL_USER)&& (tbSenha.Text.ToString()== CLAVE_TECNICA))
                        {
                            UsuarioLoggin.username = tbUsuario.Text.ToString();
                            UsuarioLoggin.pass = tbSenha.Text.ToString();
                            UsuarioLoggin.jerarquia = "superusuario";
                            
                            
                          //  MessageBox.Show("Super Usuario logeado correctamente", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.formR.DialogResult = DialogResult.OK;
                            
                        }   
                        else
                        {
                            Usuario usuarioBd = this.handler.buscarUsuarioPorUserName(tbUsuario.Text.ToString());
                            if ((usuarioBd != null) && (tbSenha.Text.ToString() == usuarioBd.Pass))
                            {
                            //    MessageBox.Show("Usuario logeado correctamente", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);


                                validarYCopiarUsuarioSession(usuarioBd);
                                
                                this.formR.DialogResult = DialogResult.OK;
                            }
                            else
                            {
                                MessageBox.Show("Usuario y/o contraseña incorrecta, vuelva a ingresar", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                voLimpiarTextboxLogin();
                                //this.formR.DialogResult = DialogResult.Cancel;
                            }
                        }

                    }
                    catch(Exception ex)
                    {
                        CLogger.Append("[btSubmit_Click] Exception: " + ex, CLogger.ERROR);      //DEBUG
                        MessageBox.Show("Error al logear usuario.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }

                }
                else
                {
                    MessageBox.Show("La cantidad de digitos es incorrecta, vuelva a ingresar los datos", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            else
            {
                MessageBox.Show("El usuario y/o la contraseña no son válidos. Vuelva a ingresar los datos correctamente", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            
            }

          
        }

        private static void validarYCopiarUsuarioSession(Usuario usuarioBd)
        {
            if ((usuarioBd.Username != null) && (!string.IsNullOrWhiteSpace(usuarioBd.Username)))
                UsuarioLoggin.username = usuarioBd.Username;
            if ((usuarioBd.Pass != null) && (!string.IsNullOrWhiteSpace(usuarioBd.Pass)))
                UsuarioLoggin.pass = usuarioBd.Pass;
            if ((usuarioBd.Jerarquia != null) && (!string.IsNullOrWhiteSpace(usuarioBd.Jerarquia)))
                UsuarioLoggin.jerarquia = usuarioBd.Jerarquia;
            if (usuarioBd.IdUsuario != null)
                UsuarioLoggin.idUsuario = usuarioBd.IdUsuario;
            if ((usuarioBd.Apellido != null) && (!string.IsNullOrWhiteSpace(usuarioBd.Apellido)))
                UsuarioLoggin.apellido = usuarioBd.Apellido;
            if ((usuarioBd.Cel != null) && (!string.IsNullOrWhiteSpace(usuarioBd.Cel)))
                UsuarioLoggin.cel = usuarioBd.Cel;
            if ((usuarioBd.Dni != null) && (!string.IsNullOrWhiteSpace(usuarioBd.Dni)))
                UsuarioLoggin.dni = usuarioBd.Dni;
            if ((usuarioBd.Email != null) && (!string.IsNullOrWhiteSpace(usuarioBd.Email)))
                UsuarioLoggin.email = usuarioBd.Email;
            if ((usuarioBd.Nombre != null) && (!string.IsNullOrWhiteSpace(usuarioBd.Nombre)))
                UsuarioLoggin.nombre = usuarioBd.Nombre;
            if ((usuarioBd.Tel != null) && (!string.IsNullOrWhiteSpace(usuarioBd.Tel)))
                UsuarioLoggin.tel = usuarioBd.Tel;
        }

        private void voLimpiarTextboxLogin()
        {
            tbSenha.Clear();
            tbUsuario.Clear();
        }
    }
}
